package com.sos.widget.view.helper;

import android.text.TextUtils;
import android.widget.TextView;

import java.util.List;

public class FormatJudge {
    private static final String REGEX_MOBILE = "^[1](([3][0-9])|([4][5,7,9])|([5][0-9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$";


    public static boolean isAllPhone(String phone) {
        if (!TextUtils.isEmpty(phone) && phone.length() == 11) {
            return true;
        }
        return false;

    }

    /**
     * 判断是否空
     *
     * @param msg
     * @return
     */
    public static boolean isEmpth(Object... msg) {
        for (Object s : msg) {
            if ("".equals(s) || null == s) {
                return false;
            }
        }
        return true;
    }


    public static boolean isEmpth(TextView... editTexts) {
        for (TextView text : editTexts) {
            if (!isEmpth(text.getText().toString().trim())) {
                return false;
            }
        }
        return true;
    }

    /**
     * @param email
     * @return 邮箱判断
     */
    public static boolean isEmail(String email) {
        String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        return email.matches(check);
    }

    public static boolean isEmpth(List<TextView> views) {
        for (TextView text : views) {
            if (!isEmpth(text.getText().toString())) {
                return false;
            }
        }
        return true;
    }

    /**
     * 手机中间四位隐藏
     *
     * @param phone
     * @return
     */
    public static String encryption(String phone) {

        if (phone == null) {
            return "";
        }
        StringBuffer stringBuffer = new StringBuffer(phone);
        int length = stringBuffer.length();
        if (length > 5) {
            stringBuffer.replace(3, length - 2, "***");
            return stringBuffer.toString();
        } else if (length > 3) {
            stringBuffer.replace(1, length - 1, "***");
            return stringBuffer.toString();
        }
        return stringBuffer.toString();
//        return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }

    public static String ifPhoneEncryption(String phone) {
        if (TextUtils.isEmpty(phone)) {
            return "";
        } else {
            if (phone.matches(REGEX_MOBILE)) {
                return encryption(phone);
            } else {
                if(phone.length()>13){
                    return phone.substring(0,6);
                }
                return phone;
            }
        }
    }

    public static boolean isUrl(String url) {
//       String urlRule= "((http|ftp|https)://)(([a-zA-Z0-9\\\\._-]+\\\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\\\.[0-9]{1,3}\\\\.[0-9]{1,3}\\\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\\\&%_\\\\./-~-]*)?";
//        if(url.contains("http")||url.contains("https")))
        return true;
    }
}
